package com.imay.live.utils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Build;
import android.support.annotation.IdRes;
import android.view.View;

import com.imay.live.R;

/**
 * Created by  nick on 2017/3/29 0029.
 */

public class ViewUtil {

    public static <V extends View> V findView(Activity activity, @IdRes int id) {
        //noinspection unchecked
        return (V) activity.findViewById(id);
    }

    public static <V extends View> V findView(View view, @IdRes int id) {
        //noinspection unchecked
        return (V) view.findViewById(id);
    }

    //渐变显示动画
    public static void startVisibilityAnimation(View view, int visibility) {
        if (view.getVisibility() != visibility) {   //当前view已经显示则不执行动画
            view.setVisibility(visibility);
            if (visibility == view.VISIBLE) {
                view.startAnimation(AnimationUtil.getAlphaAnimation(0, 1, 200, 0));
            } else {
                view.startAnimation(AnimationUtil.getAlphaAnimation(1, 0, 200, 0));
            }
        }
    }

    /**
     * 设置view圆弧
     *
     * @param view
     * @param r
     * @param color
     */
    public static void setRadius(View view, float r, int color) {
        int borderWidth = 0;// 加边框后会出现空心圆角矩形的效果，所以设置为0
        float[] outerRadius = new float[8];
        float[] innerRadius = new float[8];
        for (int i = 0; i < 8; i++) {
            outerRadius[i] = r + borderWidth;
            innerRadius[i] = r;
        }
        ShapeDrawable shapeDrawable = // 创建图形drawable
                new ShapeDrawable(// 创建圆角矩形
                        new RoundRectShape(outerRadius,
                                new RectF(borderWidth, borderWidth, borderWidth, borderWidth),
                                innerRadius));
        shapeDrawable.getPaint().setColor(color);// 使用指定的颜色绘制，即背景颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            // 高版本SDK使用新的API
            view.setBackground(shapeDrawable);
        } else {
            view.setBackgroundDrawable(shapeDrawable);
        }
    }

    /**
     * 设置view圆角
     *
     * @param view
     */
    public static void setForgroundRadius(View view) {
        if (view == null) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            view.setForeground(view.getResources().getDrawable(R.drawable.pnl_01top_yuanjiao));
        }
    }

    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {

        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }

}
